package com.mijie.homi.admin.web.mood;

import java.io.IOException;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.mijie.core.ApplicationProperties;
import com.mijie.core.Paging;
import com.mijie.homi.admin.service.mood.MoodFigureService;
import com.mijie.homi.common.domain.mood.MoodFigure;
import com.mijie.util.FileUploadUtil;
import com.mijie.util.Helper;
import com.mijie.util.IOUtil;

@Controller
@RequestMapping("/moodfigure")
public class MoodFigureContruller {

	@Autowired
	private MoodFigureService service;
	private Logger logger = Logger.getLogger(MoodFigureContruller.class);
	
	@RequestMapping("/list")
	public String list(Paging<MoodFigure> paging){
		service.listMoodFigure(paging);
		List<MoodFigure> list = paging.getList();
		for(MoodFigure figure : list){
			if(!figure.getFigure().trim().isEmpty())
				figure.setFigure(ApplicationProperties.get("resource.url")+figure.getFigure());
		}
		return "moodfigure/list";
	}
	
	@RequestMapping("/create")
	public String create(Model model){
		return "moodfigure/create";
	}
	
	@RequestMapping("/save")
	public void save(HttpServletRequest request,HttpServletResponse response,MoodFigure figure) throws IOException{
		if(request instanceof MultipartHttpServletRequest){
			MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
			Iterator<String> iter = multipartRequest.getFileNames();
			MultipartFile file  =  null; 
			String resPath = ApplicationProperties.get("resource.path");
			String figureHome = ApplicationProperties.get("mood.figure.home");
			String fileFieldName = null;
			String size = null;
			String saveNamePrefix = Helper.genUUID();
			while(iter.hasNext()){
				fileFieldName = iter.next();
				file = multipartRequest.getFile(fileFieldName);
				size = fileFieldName.substring(fileFieldName.indexOf("_")+1);
				try{
					if(file!= null){
						if(FileUploadUtil.validate(file, "jpg","png")){
							String fileName = FileUploadUtil.upload(file, resPath+figureHome,saveNamePrefix+"_"+size+".jpg");
							if(fileName!=null){
								figure.setFigure(figureHome+fileName);
							}else{
								IOUtil.write(response, "添加失败,上传失败");
								return;
							}
						}else{
							IOUtil.write(response, "添加失败,不支持该类型文件");
							return;
						}
					}
				}catch(Exception e){
					logger.error(e.getMessage(), e);
					IOUtil.write(response, "添加失败,上传中出现异常");
					return;
				}
			}
		}
		if(figure.getFigureAuthor()==null) figure.setFigureAuthor("");
		if(figure.getWhisperAuthor()==null) figure.setWhisperAuthor("");
		figure.setCreateAt(new Date());
		int succ = service.addMoodFigure(figure);
		IOUtil.write(response, succ==1?"添加成功":"添加失败");
	}
	
	@RequestMapping("/edit")
	public String edit(Model model,int id){
		MoodFigure figure = service.getMoodFigureById(id);
		model.addAttribute("figure",figure);
		return "moodfigure/edit";
	}
	
	@RequestMapping("/update")
	public void update(HttpServletRequest request,HttpServletResponse response,MoodFigure figure) throws IOException{
		if(request instanceof MultipartHttpServletRequest){
			MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
			Iterator<String> iter = multipartRequest.getFileNames();
			MultipartFile file  =  null; 
			String resPath = ApplicationProperties.get("resource.path");
			String figureHome = ApplicationProperties.get("mood.figure.home");
			String fileFieldName = null;
			String size = null;
			String saveNamePrefix = Helper.genUUID();
			while(iter.hasNext()){
				fileFieldName = iter.next();
				file = multipartRequest.getFile(fileFieldName);
				size = fileFieldName.substring(fileFieldName.indexOf("_")+1);
				try{
					if(file!= null){
						if(FileUploadUtil.validate(file, "jpg","png")){
							String fileName = FileUploadUtil.upload(file, resPath+figureHome,saveNamePrefix+"_"+size+".jpg");
							if(fileName!=null){
								figure.setFigure(figureHome+fileName);
							}else{
								IOUtil.write(response, "修改失败,上传失败");
								return;
							}
						}else{
							IOUtil.write(response, "修改失败,不支持该类型文件");
							return;
						}
					}
				}catch(Exception e){
					logger.error(e.getMessage(), e);
					IOUtil.write(response, "修改失败,上传中出现异常");
					return;
				}
			}
		}
		int succ = service.updateMoodFigure(figure);
		IOUtil.write(response, succ==1?"修改成功":"修改失败");
	}
	
	@RequestMapping("/delete")
	public String delete(int id){
		service.deleteMoodFigureById(id);
		return "redirect:/moodfigure/list.shtml";
	}
}
